import {Native} from "./native.js";

/**
 *
 * 网络状态枚举
 * @enum
 * @since 2
 * @readonly
 * @see wifi.currentApInfo
 * @see WiFiApInfo
 */
const NetWorkState = {
    /** 当前使用的是WiFi */
    WIFI: "wifi",
    /** 当前使用的是移动网络 */
    CELLULAR: "cellular",
    /** 当前手机没有连接网络 */
    NONE: "none"
};


/**
 * WiFi相关
 * @static
 * @author GuoHua Liu
 * @copyright Copyright © 2019 GuoHua Liu. All rights reserved.
 */
class wifi {
    /**
     * @private
     * @param {string} name
     * @param {function} onevent
     * @param {*} context
     */
    static addObserver(name, onevent, context) {
        Native.getInstance().addObserver("wifi/" + name, onevent, context);
        return this;
    }

    /**
     * @private
     * @param {string} name
     * @param {string} onevent
     */
    static removeObserver(name, onevent) {
        Native.getInstance().delObserver("wifi/" + name, onevent);
        return this;
    }

    /**
     * @since 2
     * @typedef NetWorkStateObserverEventData
     * @property {NetWorkState} state 网络状态
     */


    /**
     * @since 2
     * @typedef NetWorkStateObserverEvent
     * @property {NetWorkStateObserverEventData} data
     * @property {string} name
     */

    /**
     * @since 2
     * @callback NetWorkStateObserver
     * @param {NetWorkStateObserverEvent} event
     */

    /**
     * 移除观察者：观察网络状态变化事件
     * @since 2
     * @param {NetWorkStateObserver} onevent
     */
    static removeNetWorkStateObserver(onevent) {
        return wifi.removeObserver("state", onevent);
    }

    /**
     * 添加观察者：观察手机网络状态变化事件
     * @since 2
     * @param {NetWorkStateObserver} onEvent
     */
    static addNetWorkStateObserver(onEvent, context) {
        return wifi.addObserver("state", onEvent, context);
    }

    /**
     * WiFi连接上的热点信息
     * @since 2
     * @typedef {Object} WiFiApInfo
     * @property {NetWorkState} state WiFi状态
     * @property {string} ip 当前IP，如果没有连接此属性无意义
     * @property {string} ssid 当前ap的名称，如果没有连接WiFi此属性无意义
     * @property {string} bssid 当前ap的mac地址，如果没有连接WiFi此属性无意义
     */

    /**
     * 获取当前热点信息
     * @since 2
     * @returns {WiFiApInfo} WiFi信息
     */
    static currentApInfo() {
        if (Native.getInstance().isHos()) {
            return Native.getInstance().syncCallNative("wifi", {"method": "info"});
        }
        return {state: NetWorkState.NONE, ssid: "", bssid: "", ip: ""};

    }

}

export {wifi, NetWorkState};
